import { PluginObject } from 'vue';

const AiChatPlugin: PluginObject<never> = {
  install(Vue) {
    if (window._ai_chap) return;

    const script = document.createElement('script');
    script.async = true;
    script.charset = 'UTF-8';
    script.id = 'ai_chat_js';
    script.dataset.json = JSON.stringify({
      robot_key: process.env.VUE_APP_ROBOT_KEY || 'xFJsTsySbU',
      language: 'zh-CN'
    });

    script.src = process.env.VUE_APP_CHAT_SDK_URL || 
                 'http://192.168.17.3:18082/sdk/ai-chat-sdk.umd.cjs';

    // 添加事件监听
    script.addEventListener('load', () => {
      console.log('SDK loaded');
      window._ai_chap?.init();
    });

    script.addEventListener('error', (err) => {
      console.error('SDK load failed:', err);
    });

    document.head.appendChild(script);
  }
};

export default AiChatPlugin;

